﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PeekService;

namespace PeekWebApp.handler
{
    /// <summary>
    /// UserRegisterHandler 的摘要说明
    /// </summary>
    public class UserRegisterHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";            
            try
            {
                if (context.Request.UrlReferrer.AbsolutePath.Substring(context.Request.UrlReferrer.AbsolutePath.LastIndexOf('/') + 1).Equals("register.html") && context.Request.UrlReferrer.Authority.Equals(CommonConfig.SiteAlias))
                {
                    string uname = context.Request.Form["userName"];
                    string password = context.Request.Form["password"];
                    string email = context.Request.Form["email"];
                    UserRegistry userRegistry = new UserRegistry();
                    string guid = Guid.NewGuid().ToString();
                    userRegistry.Code = guid;
                    string confirmPwd = CreateRondomStr();
                    userRegistry.ConfirmPassWord = DecryptService.Encrypt(confirmPwd);
                    userRegistry.IsEmailVerification = false;
                    userRegistry.PassWord = DecryptService.Encrypt(password);
                    userRegistry.Sort = new ExtendService().GetSort("UserRegistry");
                    userRegistry.State = 0;
                    userRegistry.Remark = "";
                    userRegistry.CreateID = "";
                    userRegistry.CreateTime = DateTime.Now;
                    userRegistry.UserName = DecryptService.Encrypt(uname);
                    userRegistry.Email = DecryptService.Encrypt(email);
                    new UserRegistryService().Add(userRegistry);                 
                    string mailContent = "<div style='margin:0px auto; text-align:center; color:Red;  font-weight:bold; height:200px;  clear:left; top:180; position:relative;' >欢迎您注册为http://"+CommonConfig.SiteAlias+"网站会员，请点击下面链接激活账号，谢谢！<br/><center><a href='http://" + CommonConfig.SiteAlias + "/active.aspx?user=" + uname + "&id=" 
                        + userRegistry.ConfirmPassWord + "' style='color:red;size:18px;font-weight:bold;'>点此激活</a></center></div>";
                    CommonService.SendEmail(email, mailContent,"尊敬的" + uname + "，您好！", SendType.Sys);
                    context.Response.Redirect("/registersuccess.html?u="+uname+"&p="+userRegistry.Code);
                }
                else
                {
                   context.Response.Redirect("/register.html");
                }
            }
            catch(Exception ex)
            {
                throw ex;
                context.Response.Redirect("/register.html");

            }



        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }


        /// <summary>
        /// 创建随记验证码
        /// </summary>
        /// <returns></returns>
        private string CreateRondomStr()
        {

            string c = "";
            string str = "defghijklmnopqrstuvwxyzDEFGHIJKLMNOPQRSTUVWXYZ";
            for (var i = 1; i <4; i++)
            {
                int num = (int)Math.Round(double.Parse((str.Length - 1).ToString()));
                c += str.Substring(num, 1);
            }
            return c + DecryptService.Encrypt(DateTime.Now.ToString("yyMddHHmss"));

        }
    }
}